package com.itheima.leyou.web;

import com.itheima.leyou.common.dto.PageDTO;
import com.itheima.leyou.po.SpecGroup;
import com.itheima.leyou.po.SpuDetail;
import com.itheima.leyou.service.SkuService;
import com.itheima.leyou.service.SpuDetailService;
import com.itheima.leyou.service.SpuService;
import com.leyou.item.dto.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private SpuService spuService;

    @Autowired
    private SkuService skuService;

    @Autowired
    private SpuDetailService spuDetailService;


    /**
     * 分页查询商品信息
     * @param page
     * @param rows
     * @param saleable
     * @param brandId
     * @param categoryId
     * @param id
     * @return
     */
    @GetMapping("/spu/page")
    public ResponseEntity<PageDTO<SpuDTO>> queryPage(
            @RequestParam(value = "page",defaultValue = "1")Integer page,
            @RequestParam(value = "rows",defaultValue = "10")Integer rows,
            @RequestParam(value = "saleable",required = false)Boolean saleable,
            @RequestParam(value = "brandId",required = false)Long brandId,
            @RequestParam(value = "categoryId",required = false)Long categoryId,
            @RequestParam(value = "id",required = false)Long id){
        return ResponseEntity.ok(spuService.queryPage(page,rows,saleable,brandId,categoryId,id));
    }


    /**
     * 根据商品id（spuId）查询对应的规格参数组集合以及组内参数的信息，及对应值
     * @param spuId
     * @param searching
     * @return
     */
    @GetMapping("/spec/value")
    public ResponseEntity<List<SpecParamDTO>> listSpec(
            @RequestParam("id")Long spuId,
            @RequestParam(value = "searching",required = false)Boolean searching){

        return ResponseEntity.ok(this.spuService.listSpec(spuId,searching));
    }


    /**
     * 根据id查询商品
     * @param id
     * @return
     */
    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDTO> querySpuById(
            @PathVariable("id")Long id){
        return ResponseEntity.ok(spuService.querySpuById(id));
    }


    /**
     * 根据spuID查询sku的集合
     * @param id
     * @return
     */
    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>> querySkuByspuId(
            @RequestParam("id")Long id){
        List<SkuDTO> skuDTOList =  skuService.querySkuByspuId(id);
        return ResponseEntity.ok(skuDTOList);
    }



    /**
     * 根据id批量查询sku
     * @param ids
     * @return
     */
    @GetMapping("/sku/list")
    public ResponseEntity<List<SkuDTO>> querySkuByIds(
            @RequestParam("ids")List<Long> ids){
        List<SkuDTO> skuDTOList =  skuService.querySkuByIds(ids);
        return ResponseEntity.ok(skuDTOList);
    }


    /**
     * 根据id查询商品详情
     * @param sid
     * @return
     */
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> queryDetailById(
            @RequestParam("id")Long sid){
        SpuDetailDTO spuDetailDTOS =  spuDetailService.queryDetailById(sid);
        return ResponseEntity.ok(spuDetailDTOS);
    }


    /**
     * 根据id查询spu及sku、spuDetail等
     * @param spuId
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity<SpuDTO> queryGoods(
            @PathVariable("id")Long spuId){
        SpuDTO spuDTOS =  spuService.queryGoods(spuId);
        return ResponseEntity.ok(spuDTOS);
    }

    /**
     * 新增商品
     * @param spuDTO
     * @return
     */
    @PostMapping("/spu")
    public ResponseEntity<Void> addSpu(@RequestBody SpuDTO spuDTO){
        spuService.addSpu(spuDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改商品
     * @param spuDTO
     * @return
     */
    @PutMapping("/spu")
    public ResponseEntity<Void> updateSpu(@RequestBody SpuDTO spuDTO){
        this.spuService.updateSpu(spuDTO);
        return ResponseEntity.ok(null);
    }


    /**
     * 商品的上下架修改
     * @param id
     * @param saleable
     * @return
     */
    @PutMapping("/saleable")
    public ResponseEntity<Void> updateUpOrDownSpu(@RequestParam("id")Long id,
        @RequestParam("saleable")Boolean saleable){
        spuService.updateUpOrDownSpu(id,saleable);
        return ResponseEntity.ok().build();
    }


    /**
     * 根据skuId批量减库存
     * @param skuDTO
     * @return
     */
    @PutMapping("/stock/minus")
    public ResponseEntity<Void> deleteSpuBySku(@RequestBody SkuDTO skuDTO){
        skuService.deleteSpuBySku(skuDTO);
        return ResponseEntity.ok(null);
    }


}
